package org.sbbs.app.demo.webapp.action;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.sbbs.app.demo.model.DictionaryItem;
import org.sbbs.app.demo.service.DictionaryItemManager;
import org.sbbs.base.pagequery.hibernate.CriteriaUtil;
import org.sbbs.base.pagequery.hibernate.CustomCriteria;
import org.sbbs.base.pager.IPager;
import org.sbbs.base.pager.impl.JqgridPagerImpl;
import org.sbbs.base.searcher.SearchFilter;
import org.sbbs.base.searcher.SearchFilterBuilder;
import org.sbbs.base.searcher.SearchOperator;
import org.sbbs.base.webapp.action.BaseJqgridPagedAction;

public class DictionaryItemPagedAction
    extends BaseJqgridPagedAction {
    private String dictionaryId;

    DictionaryItemManager dictionaryItemManager;

    public DictionaryItemManager getDictionaryItemManager() {
        return dictionaryItemManager;
    }

    public void setDictionaryItemManager( DictionaryItemManager dictionaryItemManager ) {
        this.dictionaryItemManager = dictionaryItemManager;
    }

    public String getDictionaryId() {
        return dictionaryId;
    }

    public void setDictionaryId( String dictionaryId ) {
        this.dictionaryId = dictionaryId;
    }

    @Override
    public Class getSearchEntityClazz() {
        return DictionaryItem.class;
    }

    /*
     * @Override public String gridPageList() { this.setSearchFilterType(SearchFilterBuilder.JQGRID_SELF); return
     * super.gridPageList(); }
     */

    public String gridPageList() {
        this.setSearchFilterType( SearchFilterBuilder.PROPERTY_FILTER );
        this.setCustomCriteria( new DictionaryItemCustomCriteria( this.dictionaryId ) );
        try {
            IPager pager = new JqgridPagerImpl( getRequest() );
            SearchFilter sf = SearchFilterBuilder.buildSearchFilter( getRequest(), this.getSearchFilterType() );
            if ( this.getCustomCriteria() != null )
                pager = this.dictionaryItemManager.pagedQuery( this.getSearchEntityClazz(), pager, sf, this.getCustomCriteria() );
            else
                pager = this.dictionaryItemManager.pagedQuery( this.getSearchEntityClazz(), pager, sf );

            this.pageList = pager.getList();
            records = pager.getTotalNumberOfRows();
            total = pager.getTotalNumberOfPages();
            page = pager.getPageNumber();
        }
        catch ( Exception e ) {
            log.error( "GridList Error", e );
        }
        return SUCCESS;
    }
}

class DictionaryItemCustomCriteria
    implements CustomCriteria {
    private String parentId;

    DictionaryItemCustomCriteria( String parentId ) {
        this.parentId = parentId;
    }

    @Override
    public Criteria getCustomCriteria( final Class clazz, Session session )
        throws Exception {
        Criteria cr = session.createCriteria( clazz );
        Criterion crn = CriteriaUtil.createCriterion( clazz, "dictionary.dictionaryId", SearchOperator.eq.getOpr(), parentId );
        return cr.add( crn );
    }

}
